package bairui.international.weixin.portal.miniapp.controller.user;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.querydsl.core.BooleanBuilder;

import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.status.State_ElectronicTicket;
import bairui.international.model.type.Type_User;
import bairui.international.service.Service_BranchCompanyStaff;
import bairui.international.service.Service_ElectronicTicket;
import bairui.international.service.Service_User;
import bairui.international.weixin.portal.miniapp.controller.branchCompany.staff.C_BranchCompnay_Staff_ShowQRImg_ShowImg;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 *
 * 得到用户信息
 *
 * @generated
 * 
 * @see STest_C_User_GetInfo_Get 测试
 *
 */
@Controller
public class C_User_GetInfo_Get
 extends SC_User_GetInfo_Get 
{
	
	@Autowired private Service_User service_User;
	@Autowired private Service_ElectronicTicket service_ElectronicTicket;
	@Autowired private Service_BranchCompanyStaff service_BranchCompanyStaff;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@GetMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request,
		@RequestParam String skey
	) throws Exception {
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		long ticketCount = service_ElectronicTicket.count(it->
				new BooleanBuilder()
				.or(it.assignedUser.eq(user).and(it.state.eq(State_ElectronicTicket.Assigned)))
				.or(it.takor.eq(user).and(it.state.eq(State_ElectronicTicket.Taked)))
			);
		TB_BranchCompanyStaff branchCompanyStaff = service_BranchCompanyStaff.getActive(user);
		return new Output(
				user.getId(),
				user.getNickName(),
				user.getRealName(),
				user.getType().getComment(),
				user.getType(),
//				user.getDimensionalUrl(),
				branchCompanyStaff == null ? null : new C_BranchCompnay_Staff_ShowQRImg_ShowImg.ShowImg.Operate() {}.operate().getRawUrl() + "?skey=" + skey,
				user.getUserAmount().getGrade(),
				ticketCount,
				branchCompanyStaff == null ? null : branchCompanyStaff.getBranchCompany().getName(),
				branchCompanyStaff == null ? null : branchCompanyStaff.getId(),
				branchCompanyStaff == null ? false : true,
				branchCompanyStaff == null ? false : branchCompanyStaff.getDirectorFlag()
			);
	}
	
	@AllArgsConstructor
	public static class Output {
		private @Getter long id;
		private @Getter String nickName;
		private @Getter String realName;
		private @Getter String type;
		private @Getter Type_User userType;
		private @Getter String dimensionalUrl;
		private @Getter Double grade;
		private @Getter long ticketCount;
		//
		private @Getter String branchCompanyName;
		private @Getter Long branchCompanyStaffId;
		/** 分公司员工标志 */
		private @Getter boolean branchCompanyStaffFlag;
		/** 分公司负责人标志 */
		private @Getter boolean branchCompanyDirectorFlag;
	}
	
}
